package com.badboy.mycarbon.login.controller;

import com.badboy.mycarbon.login.common.ResponseResult;
import com.badboy.mycarbon.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;

    // 登录接口
    @PostMapping("/login")
    public ResponseResult<Map<String, Object>> login(@RequestBody Map<String, String> params) {
        Map<String, Object> result = loginService.login(
                params.get("accountName"),
                params.get("password")
        );
        return ResponseResult.success(result);
    }

    // 退出登录接口
    @PostMapping("/logout")
    public ResponseResult<?> logout(@RequestParam Long accountId) {
        loginService.logout(accountId);
        return ResponseResult.success();
    }

    // 注册接口
    @PostMapping("/register")
    public ResponseResult<?> register(@RequestBody Map<String, String> params) {
        loginService.register(
                params.get("accountName"),
                params.get("username"),
                params.get("password"),
                params.get("confirmPassword"),
                params.get("phone"),
                params.get("verifyCode")
        );
        return ResponseResult.success();
    }

    // 发送验证码接口
    @PostMapping("/sendCode")
    public ResponseResult<?> sendCode(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String scene = params.get("scene");
        String message = loginService.sendPhoneCode(phone, scene);
        return ResponseResult.success(message);
    }

    // 忘记密码接口
    @PostMapping("/forgotPassword")
    public ResponseResult<?> forgotPassword(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String verifyCode = params.get("verifyCode");
        String newPassword = params.get("newPassword");
        boolean isSuccess = loginService.forgotPassword(phone, verifyCode, newPassword);
        if (isSuccess) {
            return ResponseResult.success("密码重置成功");
        } else {
            return ResponseResult.fail(500,"密码重置失败");
        }
    }
}